﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using WRJClient.ViewModel;

namespace WRJClient.UI.SystemMonitor
{
    public class TopoTreeNodeItem : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public Brush Color
        {
            get
            {
                Brush color = Brushes.White;
                if (ViewModel != null)
                {

                    switch (ViewModel.GetType().Name)
                    {
                        case "UAVViewModel":
                            UAVViewModel uav = ViewModel as UAVViewModel;
                            if (uav != null)
                            {
                                if (uav.IsOnline)
                                {
                                    color = Brushes.White;
                                }
                                else
                                {
                                    color = Brushes.Gray;
                                }
                            }
                            break;
                        case "WorkStationViewModel":
                            WorkStationViewModel workstation = ViewModel as WorkStationViewModel;
                            if (workstation != null)
                            {
                                if (workstation.IsOnline)
                                {
                                    color = Brushes.White;
                                }
                                else
                                {
                                    color = Brushes.Gray;
                                }
                            }
                            break;
                        case "ControlCenterViewModel":
                            ControlCenterViewModel controlcenter = ViewModel as ControlCenterViewModel;
                            if (controlcenter != null)
                            {
                                if (controlcenter.IsOnline)
                                {
                                    color = Brushes.White;
                                }
                                else
                                {
                                    color = Brushes.Gray;
                                }
                            }
                            break;
                        case "MissionViewModel":
                            color = Brushes.Yellow;
                            break;
                    }
                }
                return color;
            }
        }

        public string Icon
        {
            get
            {
                string icon = "";
                if (ViewModel != null)
                {

                    switch (ViewModel.GetType().Name)
                    {
                        case "UAVViewModel":
                            UAVViewModel uav = ViewModel as UAVViewModel;
                            if (uav != null)
                            {
                                if (uav.IsOnline)
                                {
                                    icon = "/Resource/Image/ti_u0.png";
                                }
                                else
                                {
                                    icon = "/Resource/Image/ti_u1.png";
                                }
                            }
                            break;
                        case "WorkStationViewModel":
                            WorkStationViewModel workstation = ViewModel as WorkStationViewModel;
                            if (workstation != null)
                            {
                                if (workstation.IsOnline)
                                {
                                    icon = "/Resource/Image/ti_w0.png";
                                }
                                else
                                {
                                    icon = "/Resource/Image/ti_w1.png";
                                }
                            }
                            break;
                        case "ControlCenterViewModel":
                            ControlCenterViewModel controlcenter = ViewModel as ControlCenterViewModel;
                            if (controlcenter != null)
                            {
                                if (controlcenter.IsOnline)
                                {
                                    icon = "/Resource/Image/ti_c0.png";
                                }
                                else
                                {
                                    icon = "/Resource/Image/ti_c1.png";
                                }
                            }
                            break;
                        case "MissionViewModel":
                            icon = "/Resource/Image/ti_m0.png";
                            break;
                    }
                }
                return icon;
            }
        }
        public string Type
        {
            get
            {
                if (ViewModel != null)
                {
                    return ViewModel.GetType().Name;
                }
                return "";
            }
        }

        public object ViewModel { get; set; } = null;

        private string _displayName = "";
        public string DisplayName
        {
            get
            {
                string displayName = _displayName;
                if (ViewModel != null)
                {
                    switch (ViewModel.GetType().Name)
                    {
                        case "UAVViewModel":
                            UAVViewModel uav = ViewModel as UAVViewModel;
                            if (uav != null)
                            {
                                displayName = uav.StaticData.UavId;
                            }
                            break;
                        case "WorkStationViewModel":
                            WorkStationViewModel workstation = ViewModel as WorkStationViewModel;
                            if (workstation != null)
                            {
                                displayName = string.Format("{0}[{1}]", workstation.StaticData.Name, workstation.StaticData.WorkStationId);
                            }
                            break;
                        case "ControlCenterViewModel":
                            ControlCenterViewModel controlcenter = ViewModel as ControlCenterViewModel;
                            if (controlcenter != null)
                            {
                                displayName = string.Format("{0}[{1}]", controlcenter.StaticData.Name, controlcenter.StaticData.WorkStationId);
                            }
                            break;
                        case "MissionViewModel":
                            MissionViewModel mission = ViewModel as MissionViewModel;
                            if (mission != null)
                            {
                                displayName = string.Format("{0}", mission.StaticData.Name);
                            }
                            break;
                        case "InfomationViewModel":
                            InfomationViewModel infomation = ViewModel as InfomationViewModel;
                            if (infomation != null)
                            {
                                displayName = infomation.StaticData.Name;
                            }
                            break;
                        case "ZoneViewModel":
                            ZoneViewModel zone = ViewModel as ZoneViewModel;
                            if (zone != null)
                            {
                                displayName = zone.StaticData.Name;
                            }
                            break;
                        case "AirlineViewModel":
                            AirlineViewModel airline = ViewModel as AirlineViewModel;
                            if (airline != null)
                            {
                                displayName = airline.StaticData.Name;
                            }
                            break;
                    }
                }
                return displayName;
            }
        }

        public ObservableCollection<TopoTreeNodeItem> Children { get; set; }
        public TopoTreeNodeItem()
        {
            Children = new ObservableCollection<TopoTreeNodeItem>();
        }

        public TopoTreeNodeItem(string name)
        {
            Children = new ObservableCollection<TopoTreeNodeItem>();
            _displayName = name;
        }

        public TopoTreeNodeItem(ControlCenterViewModel vm)
        {
            vm.PropertyChanged += VmOnPropertyChanged;
            Children = new ObservableCollection<TopoTreeNodeItem>();
            ViewModel = vm;
        }


        public TopoTreeNodeItem(WorkStationViewModel vm)
        {
            vm.PropertyChanged += VmOnPropertyChanged;
            Children = new ObservableCollection<TopoTreeNodeItem>();
            ViewModel = vm;
        }
        public TopoTreeNodeItem(UAVViewModel vm)
        {
            vm.PropertyChanged += VmOnPropertyChanged;
            Children = new ObservableCollection<TopoTreeNodeItem>();
            ViewModel = vm;
        }
        public TopoTreeNodeItem(MissionViewModel vm)
        {
            Children = new ObservableCollection<TopoTreeNodeItem>();
            ViewModel = vm;
        }
        public TopoTreeNodeItem(InfomationViewModel vm)
        {
            Children = new ObservableCollection<TopoTreeNodeItem>();
            ViewModel = vm;
        }
        public TopoTreeNodeItem(AirlineViewModel vm)
        {
            Children = new ObservableCollection<TopoTreeNodeItem>();
            ViewModel = vm;
        }
        public TopoTreeNodeItem(ZoneViewModel vm)
        {
            Children = new ObservableCollection<TopoTreeNodeItem>();
            ViewModel = vm;
        }

        private void VmOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
        {
            if (propertyChangedEventArgs.PropertyName == "IsOnline")
            {
                OnPropertyChanged("Color");
                OnPropertyChanged("Icon");
            }
        }
    }
}
